# -*- coding: utf-8 -*-
"""
------------------------------------------------------------------------------
    File Name:  json_demo
    Author   :  wanwei1029
    Date     :  2018-10-17
    Desc     :
------------------------------------------------------------------------------
"""
import json


def dump(file_name):
    dict_obj = dict()
    dict_obj['张三'] = 1
    dict_obj['李四'] = 21
    dict_obj['王五'] = 3
    dict_obj['赵六'] = 4
    dict_obj['孙七'] = 5
    dict_obj['王八'] = 6
    with open(file_name, "w+", encoding="utf-8") as f:
        json.dump(dict_obj, f, ensure_ascii=False, indent=2)


def load(file_name):
    with open(file_name, "r", encoding="utf-8") as f:
        dict_obj = json.load(f)
        print(dict_obj)
        print(type(dict_obj))


def str_trans():
    """
    转换成字符串
    :return:
    """
    dict_obj = dict()
    dict_obj['张三'] = 1
    dict_obj['李四'] = 21
    dict_obj['王五'] = 3
    dict_obj['赵六'] = 4
    dict_obj['孙七'] = 5
    dict_obj['王八'] = 6
    json_str = json.dumps(dict_obj, ensure_ascii=False,)
    print(json_str)
    print(type(json_str))
    new_dict = json.loads(json_str)
    print(new_dict)
    print(type(new_dict))


def demo():
    """
    """


if __name__ == '__main__':
    test_method = "str_trans"
    if test_method == "dump":
        dump("data/dump_json.txt")
    elif test_method == "load":
        load("data/dump_json.txt")
    elif test_method == "str_trans":
        str_trans()
